<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="@/assets/logo.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主题 -->
    <el-container>
      <!-- 侧边 -->
      <el-aside :width="isCollapse ? '60px' : '200px'">
        <div class="toggle-btn" @click="toggleCollapse">|||</div>
        <!-- 菜单 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="active">
          <!-- 一级 -->
          <el-submenu :index="'' + item.id" v-for="item in menuList" :key="item.id">
            <!-- 内容 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级选项 -->
            <el-menu-item :index="'/home/' + val.path" v-for="val in item.children" :key="val.id" @click="saveNavState('/home/' + val.path)">
              <i class="el-icon-menu"></i>
              <span>{{ val.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getMenuList } from '@/api'
export default {
  name: 'HomeIndex',
  data() {
    return {
      // 菜单数据
      menuList: [],
      // 字体图标项
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 折叠控制
      isCollapse: false,
      active: sessionStorage.getItem('active') || ''
    }
  },
  methods: {
    // 退出
    logout() {
      sessionStorage.removeItem('token')
      this.$router.replace('/login')
    },
    // 控制菜单栏折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(active) {
      this.active = active
    }
  },
  async created() {
    const { data: res } = await getMenuList()
    if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    this.menuList = res.data
    console.log(this.menuList)
  }
}
</script>

<style lang="less" scoped>
.el-container {
  user-select: none;
  height: 100%;
  overflow: hidden;
  .el-header {
    padding-left: 0;
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
      height: 100%;
      display: flex;
      align-items: center;
      img {
        height: 100%;
      }
      span {
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    overflow: hidden;
    .el-menu {
      border-right: none;
    }
    .toggle-btn {
      cursor: pointer;
      background-color: #4a5064;
      text-align: center;
      line-height: 22px;
      color: #fff;
      font-size: 10px;
      letter-spacing: 0.2em;
    }
  }
  .el-main {
    background-color: #eaedf1;
    height: 100%;
    background: url(https://uploadfile.bizhizu.cn/2017/0731/20170731103323296.jpg) no-repeat;
    background-size: cover;
  }
  .iconfont {
    margin-right: 10px;
  }
}
</style>
